package cn.anjubao.cp.api.pay.utils;

import cn.anjubao.cp.api.common.BussinessType;
import cn.anjubao.cp.api.common.Constants;
import cn.anjubao.cp.api.dto.ParkingRecordPayRecord;
import cn.anjubao.cp.api.dto.unifiedPayReqForm;
import cn.anjubao.cp.api.entity.ParkingRecord;
import cn.anjubao.cp.api.entity.PayRecord;
import cn.anjubao.cp.api.entity.TradeOrder;
import cn.anjubao.cp.api.mapper.ParkingRecordMapper;
import cn.anjubao.cp.api.mapper.PayRecordMapper;
import cn.anjubao.cp.api.mapper.TradeOrderMapper;
import cn.anjubao.cp.api.pay.PaymentService;
import cn.anjubao.cp.common.base.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Component
public class PayUtils {

    private static final Logger logger = LoggerFactory.getLogger(PayUtils.class);

    @Autowired
    private PayRecordMapper payRecordMapper;
    @Autowired
    private TradeOrderMapper tradeOrderMapper;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;

    /**
     * 封装tradeOrder
     * @param type
     * @return
     */
    public TradeOrder packTradeOrder(unifiedPayReqForm reqForm,TradeOrder tradeOrder,int type) throws BusinessException {
        if (type== Constants.lowPolePay){
            PayRecord oldPayRecord=payRecordMapper.getByParkingRecordId(reqForm.getParkingRecordId(),0);
            tradeOrder.setPayRecordId(oldPayRecord.getId());
            tradeOrder.setParkingRecordId(reqForm.getParkingRecordId());
            tradeOrder.setOrderType(2);
            tradeOrder.setPayType(3);
        }
        if (type==Constants.middlePolePrePay){
            ParkingRecordPayRecord record=parkingRecordMapper.getBySlotNum(reqForm.getSlotNum());
            if (record!=null && !StringUtils.isEmpty(record.getPayRecordId())){
                throw new BusinessException("-1","中杆预缴费用已支付,无需再次支付");
            }
            tradeOrder.setParkingRecordId(record.getParkingRecordId());
            tradeOrder.setOrderType(3);
            tradeOrder.setPayType(1);
            tradeOrder.setTime(reqForm.getParkTime());
            tradeOrder.setEnableMonitor(reqForm.getEnableMonitor());
        }
        if (type==Constants.middlePoleFixPay){
            PayRecord oldPayRecord=payRecordMapper.getByParkingRecordId(reqForm.getParkingRecordId(),0);
            tradeOrder.setParkingRecordId(reqForm.getParkingRecordId());
            tradeOrder.setOrderType(3);
            tradeOrder.setPayType(2);
            tradeOrder.setPayRecordId(oldPayRecord.getId());
            tradeOrder.setTime(oldPayRecord.getTime());
            tradeOrder.setEnableMonitor(oldPayRecord.getEnableMonitor());
        }
        return tradeOrder;
    }

    /**
     *  处理余额支付返回及微信支付回调
     * @param tradeOrder
     * @param payTime
     * @param payType
     * @param paySource 支付来源 1：余额 2：在线
     */
    @Transactional
    public void handlePayCallBack(TradeOrder tradeOrder,Date payTime,int payType,int paySource){
        if (payType==Constants.lowPolePay || payType==Constants.middlePoleFixPay){
            logger.info("支付成功,支付来源：{},支付类型：{},tradeOrderId:{}",
                    paySource==Constants.balancePay?"余额支付":"在线支付",
                    payType==Constants.lowPolePay?"低杆缴费":"中杆补缴",tradeOrder.getId());
            //更新tradeOrder表
            tradeOrder.setOrderState(1);
            tradeOrder.setPayTime(payTime);
            tradeOrder.setGmtUpdate(new Date());
            tradeOrderMapper.updateById(tradeOrder);
            //更新payRecord表
            PayRecord oldPayRecord=payRecordMapper.selectById(tradeOrder.getPayRecordId());
            oldPayRecord.setUserId(tradeOrder.getUserId());
            oldPayRecord.setPayState(true);
            oldPayRecord.setPayTime(payTime);
            oldPayRecord.setPaySource(paySource);
            oldPayRecord.setGmtUpdate(new Date());
            payRecordMapper.updateById(oldPayRecord);
            //更新parkingRecord表
            ParkingRecord parkingRecord= parkingRecordMapper.selectById(tradeOrder.getParkingRecordId());
            parkingRecord.setUserId(tradeOrder.getUserId());
            parkingRecord.setFeeState(3);
            parkingRecordMapper.updateById(parkingRecord);
        }
        if (payType==Constants.middlePolePrePay){
            logger.info("支付成功,支付来源：{},支付类型：中杆预缴,tradeOrderId:{}",
                    paySource==Constants.balancePay?"余额支付":"在线支付",tradeOrder.getId());
            //更新tradeOrder表
            String payRecordId=tradeOrder.getUserId()+ BussinessType.PARKING.value+System.currentTimeMillis();
            tradeOrder.setOrderState(1);
            tradeOrder.setPayTime(payTime);
            tradeOrder.setPayRecordId(payRecordId);
            tradeOrder.setGmtUpdate(new Date());
            tradeOrderMapper.updateById(tradeOrder);
            //新建payRecord表
            PayRecord payRecord=new PayRecord();
            payRecord.setId(payRecordId);
            payRecord.setUserId(tradeOrder.getUserId());
            payRecord.setAmount(tradeOrder.getAmount());
            payRecord.setRecordType(3);
            payRecord.setPayState(true);
            payRecord.setPayTime(payTime);
            payRecord.setPaySource(paySource);
            payRecord.setPayType(1);
            payRecord.setParkingRecordId(tradeOrder.getParkingRecordId());
            payRecord.setTime(tradeOrder.getTime());
            payRecord.setEnableMonitor(tradeOrder.getEnableMonitor());
            payRecordMapper.insert(payRecord);
            //更新parkingRecord表
            ParkingRecord oldParkingRecord=parkingRecordMapper.selectById(tradeOrder.getParkingRecordId());
            oldParkingRecord.setUserId(tradeOrder.getUserId());
            oldParkingRecord.setEnableMonitor(tradeOrder.getEnableMonitor()==true?1:0);
            oldParkingRecord.setFeeState(1);
            Date afterDate = new Date(oldParkingRecord.getInTime().getTime() + tradeOrder.getTime()*1000);
            oldParkingRecord.setParkingExpireTime(afterDate);
            parkingRecordMapper.updateById(oldParkingRecord);
        }
        if (payType==Constants.rechargePay){
            logger.info("余额充值成功,tradeOrderId:{}",tradeOrder.getId());
            String payRecordId=tradeOrder.getUserId()+ BussinessType.BALANCE.value+System.currentTimeMillis();
            tradeOrder.setOrderState(1);
            tradeOrder.setPayTime(payTime);
            tradeOrderMapper.updateById(tradeOrder);
            PayRecord payRecord=new PayRecord();
            payRecord.setId(payRecordId);
            payRecord.setUserId(tradeOrder.getUserId());
            payRecord.setAmount(tradeOrder.getAmount());
            payRecord.setRecordType(1);
            payRecord.setPayState(true);
            payRecord.setPayTime(payTime);
            payRecord.setPaySource(2);
            payRecordMapper.insert(payRecord);
        }
    }
}
